/**
 * Customer实体类
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.entity;


import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.sxf.crm.enums.CustomerSource;
import com.sxf.crm.enums.CustomerStage;
import com.sxf.crm.enums.CustomerStatus;
import com.sxf.crm.enums.DeleteStatus;
import jakarta.persistence.*;
import lombok.Data;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

@Data
@Entity
@Table(name = "customers")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id; // 客户ID，主键

    @Column(name = "name", nullable = false)
    private String name; // 客户名称

    @Column(name = "email")
    private String email; // 电子邮箱

    @Column(name = "address")
    private String address; // 地址

    @Column(name = "industry_id")
    private Integer industryId; // 所属行业

    @Column(name = "description", columnDefinition = "TEXT")
    private String description; // 客户描述

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "owner_id")
    private User owner; // 客户负责人

    @Column(name = "stage")
    @Enumerated(EnumType.STRING)
    private CustomerStage stage; // 客户阶段(LEAD-潜在客户, OPPORTUNITY-机会客户, CUSTOMER-正式客户, PARTNER-合作伙伴)

    @Column(name = "status")
    @Enumerated(EnumType.STRING)
    private CustomerStatus status = CustomerStatus.PENDING; // 客户状态(PENDING-待处理, APPROVED-已审核, REJECTED-已拒绝, ARCHIVED-已归档)，默认待处理

    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private CustomerSource source; // 客户来源(WEBSITE-网站, REFERRAL-推荐, ADVERTISEMENT-广告, EXHIBITION-展会, OTHER-其他)

    @ManyToMany
    @JoinTable(
        name = "customer_industries",
        joinColumns = @JoinColumn(name = "customer_id"),
        inverseJoinColumns = @JoinColumn(name = "industry_id")
    )
    private Set<Industry> industries = new HashSet<>(); // 关联的行业集合(多对多关系，通过中间表customer_industries关联)

    @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, orphanRemoval = true)
    @JsonIgnoreProperties({"customer", "hibernateLazyInitializer", "handler"})
    private Set<CustomerDepartment> customerDepartments = new HashSet<>(); // 关联的客户部门关系(一对多关系，级联操作)

    @ManyToMany
    @JoinTable(
        name = "customer_departments",
        joinColumns = @JoinColumn(name = "customer_id"),
        inverseJoinColumns = @JoinColumn(name = "department_id")
    )
    private Set<Department> departments = new HashSet<>(); 

    @Column(name = "visit_address")
    private String visitAddress; // 拜访地址

    @Column(name = "register_address")
    private String registerAddress; // 注册地址

    @Column(name = "contact_name")
    private String contactName; // 联系人姓名

    @Column(name = "contact_phone")
    private String contactPhone; // 联系电话

    @Column(name = "contact_email")
    private String contactEmail; // 联系人邮箱

    @Column(name = "contact_position")
    private String contactPosition; // 联系人职位

    @Column(name = "website")
    private String website; // 公司网站

    @Column(name = "business_license")
    private String businessLicense; // 营业执照号

    @Column(name = "annual_revenue")
    private BigDecimal annualRevenue; // 年营业额

    @Column(name = "employee_count")
    private Integer employeeCount; // 员工人数

    @Column(name = "remark")
    private String remark; // 备注信息

    @Column(name = "delete_status", nullable = false)
    @Enumerated(EnumType.ORDINAL)
    private DeleteStatus deleteStatus = DeleteStatus.NOT_DELETED; // 删除状态(0-未删除, 1-已删除)，默认未删除

    @Column(name = "audit_comment")
    private String comment; // 审核意见

    private Long createAt;

    @Column(name = "create_user_id")
    private Long createUserId;

    private Long updateAt;

    @Column(name = "update_user_id")
    private Long updateUserId;
}
